import { Modal, Row, Select } from "antd";
import { addon, getObject, Ref } from "pao-aop";
import { reactControl, ReactView, ReactViewControl, ReactViewState } from "pao-aop-client";
import React from "react";
import { NTOperationTable } from "src/business/components/buss-components/operation-table";
import SignFrameLayout, { InputType } from "src/business/components/buss-components/sign-frame-layout";
import { request } from "src/business/util_tool";
import { AppServiceUtility } from "src/projects/app/appService";
import { table_param } from "src/projects/app/util-tool";
import { ILeaveRecordService } from "src/projects/models/leave-record";
import { ROUTE_PATH } from "src/projects/router";

const Option = Select.Option;

/**
 * 组件：住宿记录信息状态
 */
export interface LeaveRecordViewState extends ReactViewState {
    /** 选择行的Id集合 */
    select_ids?: string[];
    /** 住宿记录项目集合 */
    bed_list?: any[];
    /** 弹出框是否显示 */
    modal_visible?: boolean;
    /** 住宿记录总条数 */
    total?: number;
    /** 标题 */
    title?: string;
    /** 表格列信息 */
    columns_service_item?: any[];
    /** 接口名 */
    request_url?: string;
}

/**
 * 组件：住宿记录信息
 * 描述
 */
export class LeaveRecordView extends ReactView<LeaveRecordViewControl, LeaveRecordViewState> {
    private columns_data_source = [
        {
            title: '长者名称',
            dataIndex: 'user.name',
            key: 'user.name',
        }, {
            title: '床位编号',
            dataIndex: 'bed.name',
            key: 'bed.name',
        }, {
            title: '状态',
            dataIndex: 'status',
            key: 'status',
        }, {
            title: '入住时间',
            dataIndex: 'checkin_date',
            key: 'checkin_date',
        }, {
            title: '离开时间',
            dataIndex: 'checkout_date',
            key: 'checkout_date',
        }
    ];
    constructor(props: any) {
        super(props);
        this.state = {
            select_ids: [],
            bed_list: [],
            modal_visible: false
        };
    }
    componentDidMount() {
        // 床位编号
        request(this, AppServiceUtility.hotel_zone_type_service.get_bed_list!({}))
            .then((datas: any) => {
                this.setState({
                    bed_list: datas.result,
                });
            });
    }
    /** 住宿记录服务 */
    serviceService_Fac() {
        return getObject(this.props.serviceService_Fac!);
    }
    /** 新增按钮 */
    add = () => {
        this.props.history!.push(ROUTE_PATH.leaveEdit);
    }
    /** 模版选择框取消回调方法 */
    handleCancel = (e: any) => {
        this.setState({
            modal_visible: false,
        });
    }
    componentWillMount() {
        let request_url = this.props.request_url;
        this.setState({
            request_url
        });
    }
    render() {
        // 床位编号
        let bed_list = this.state.bed_list;
        let bed_option_list: any[] = [];
        bed_list!.map((item, idx) => {
            bed_option_list.push(<Option key={item.id}>{item.name}</Option>);
        });
        let leave_datalist: any[] = [];
        let leave_data: any[] = [
            {
                id: '离开',
                name: '离开'
            },
            {
                id: '入住',
                name: '入住'
            }
        ];
        leave_data!.map((item, idx) => {
            leave_datalist.push(<Option key={item.id}>{item.name}</Option>);
        });

        let accommodation_record = {
            type_show: false,
            edit_form_items_props: [
                {
                    type: InputType.input,
                    label: "长者名称",
                    decorator_id: "elder_name"
                },
                {
                    type: InputType.input,
                    label: "床位名称",
                    decorator_id: "bed_name",
                },
                {
                    type: InputType.select,
                    label: '状态',
                    decorator_id: "status",
                    option: {
                        childrens: leave_datalist,
                    }
                },
            ],
            btn_props: [{
                label: '新增',
                btn_method: this.add,
                icon: 'plus'
            },],
            // on_row_selection: this.on_row_selection,
            columns_data_source: this.columns_data_source,
            // on_icon_click: this.onIconClick,
            service_object: AppServiceUtility.leaveRecord_service,
            service_option: {
                select: {
                    service_func: this.state.request_url,
                    service_condition: [{}, 1, 10]
                },
                delete: {
                    service_func: 'del_leave_record'
                }
            },
            permission_class: this.props.permission_class,
            get_permission_name: this.props.get_permission_name,
            select_permission: this.props.select_permission,
            edit_permission: this.props.edit_permission,
            add_permission: this.props.add_permission,
            delete_permission: this.props.delete_permission,
        };
        let accommodation_record_list = Object.assign(accommodation_record, table_param);
        delete accommodation_record_list.other_label_type;
        return (
            <Row>
                <Modal
                    title={this.state.title}
                    visible={this.state.modal_visible}
                    onCancel={this.handleCancel}
                    width={900}
                >
                    <NTOperationTable
                        data_source={this.state.bed_list}
                        columns_data_source={this.state.columns_service_item}
                        table_size='middle'
                        showHeader={true}
                        bordered={true}
                        total={this.state.total}
                        default_page_size={10}
                        total_pages={Math.ceil((this.state.total ? this.state.total : 0) / 10)}
                        show_footer={true}
                    />
                </Modal>
                <SignFrameLayout {...accommodation_record_list} />
            </Row>
        );
    }
}

/**
 * 控件：离开原因信息
 * 描述
 */
@addon('LeaveRecordView', '离开原因信息', '描述')
@reactControl(LeaveRecordView, true)
export class LeaveRecordViewControl extends ReactViewControl {
    /** 住宿记录接口服务 */
    public serviceService_Fac?: Ref<ILeaveRecordService>;
    /** 请求接口名 */
    public request_url?: string;
    /** 获取权限接口类 */
    public permission_class?: any;
    /** 获取权限接口方法名 */
    public get_permission_name?: string;
    /** 查询权限 */
    public select_permission?: string;
    /** 编辑权限 */
    public edit_permission?: string;
    /** 删除权限 */
    public delete_permission?: string;
    /** 新增权限 */
    public add_permission?: string;

}